#include "cal.h"

int cal ( int mood, FILE* record, FILE* fp ) {
    int s1, op1, s2, key, tmp, test;
    /* que: s1 op1( 0 stands for plus, 1 stands for minus ) s2 key */
    while (1) {
        key = rand() % 100;
        if ( key == 0 ) continue;
        op1 = rand() % 2;
        /* op1 == 0 <==> plus  **
        ** op1 == 1 <==> minus */
        if ( op1 == 0 ) {
            s1 = rand() % key;
            s2 = key - s1;
            if ( s1 == 0 || s2 == 0 ) continue;
            if ( mood == 1 ) {
                printf("%2d + %2d = \n",s1,s2);
                fprintf(record,"%2d + %2d = %2d\n",s1,s2,key);
            } else if ( mood == 2 ) {
                fprintf(fp,"%2d + %2d = ", s1, s2);
            }
        } else {
            s1 = rand() % 100;
            if ( s1 == key || s1 == 0 ) continue; 
            if ( s1 < key ) {
                tmp = s1;
                s1 = key;
                key = tmp;
            }
            s2 = s1 - key;
            if ( s1 == s2 ) continue;
            if ( mood == 1 ) {
                printf("%2d - %2d = \n",s1,s2);
                fprintf(record, "%2d - %2d = %2d\n",s1,s2,key);
            } else if ( mood == 2 ) {
                fprintf(fp, "%2d - %2d = ",s1, s2);
            }
        }
        break;
    }

    return key;
}